package com.cheetah.start.system.dict.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheetah.start.common.annotation.Log;
import com.cheetah.start.common.annotation.NeedLogin;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.base.result.ResultTable;
import com.cheetah.start.common.enums.BusinessType;
import com.cheetah.start.common.enums.LoggingType;
import com.cheetah.start.common.query.PageQuery;
import com.cheetah.start.system.dict.domain.SysDictData;
import com.cheetah.start.system.dict.domain.SysDictType;
import com.cheetah.start.system.dict.service.SysDicTypeService;
import com.cheetah.start.system.dict.service.SysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.constraints.NotBlank;

/**
 * 字典类型控制器
 *
 * @author Feel
 * @date 2021/07/25
 */
@NeedLogin
@Slf4j
@Validated
@RestController
@RequestMapping("/dictType")
public class SysDictTypeController extends ResultController {
    @Autowired
    private SysDicTypeService dicTypeService;
    @Autowired
    private SysDictDataService dictDataService;

    /**
     * 列表
     *
     * @param pageQuery 页面查询
     * @return {@link ResultTable}
     */
    @GetMapping("/list")
    public ResultTable list(PageQuery pageQuery) {
        Page<SysDictType> page = new Page<>(pageQuery.getPage(), pageQuery.getLimit());
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        dicTypeService.page(page, wrapper);
        return pageTable(page.getRecords(), page.getTotal());
    }

    /**
     * 添加
     *
     * @param sysDictType sys dict类型
     * @return {@link Result}
     */
    @PostMapping("/add")
    @Log(title = "字典类型", describe = "添加", type = BusinessType.ADD, logType = LoggingType.OPERATE)
    public Result add(@Validated @RequestBody SysDictType sysDictType) {
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
        wrapper.eq("type_code", sysDictType.getTypeCode());
        SysDictType one = dicTypeService.getOne(wrapper);
        if (one != null) {
            return failure("字典类型不能重复");
        }
        boolean save = dicTypeService.save(sysDictType);
        return decide(save);
    }


    /**
     * 编辑
     *
     * @param dictTypeId dict类型id
     * @return {@link ModelAndView}
     */
    @GetMapping("/edit")
    public ModelAndView edit(String dictTypeId) {
        SysDictType dictType = dicTypeService.getById(dictTypeId);
        ModelAndView model = new ModelAndView();
        model.addObject("sysDictType", dictType);
        model.setViewName("/dict/edit");
        return model;
    }

    /**
     * 更新
     *
     * @param sysDictType sys dict类型
     * @return {@link Result}
     */
    @PostMapping("/update")
    @Log(title = "字典类型", describe = "编辑", type = BusinessType.EDIT, logType = LoggingType.OPERATE)
    public Result update(@Validated @RequestBody SysDictType sysDictType) {
        if (sysDictType.getDictTypeId() != null) {
            boolean b = dicTypeService.updateById(sysDictType);
            return decide(b, BusinessType.EDIT);
        }
        return failure("请先添加");
    }


    /**
     * 删除
     *
     * @param dictTypeId dict类型id
     * @return {@link Result}
     */
    @PostMapping("/remove/{dictTypeId}")
    @Transactional(rollbackFor = Exception.class)
    @Log(title = "字典数据", describe = "删除", type = BusinessType.REMOVE, logType = LoggingType.OPERATE)
    public Result remove(@PathVariable @NotBlank(message = "不能为空") String dictTypeId) {
        SysDictType dictType = dicTypeService.getById(dictTypeId);
        String typeCode = dictType.getTypeCode();
        boolean b = dicTypeService.removeById(dictTypeId);
        QueryWrapper<SysDictData> wrapper = new QueryWrapper<>();
        wrapper.eq("type_code", typeCode);
        boolean remove = dictDataService.remove(wrapper);
        return decide(b, BusinessType.REMOVE);
    }

    /**
     * 启用
     *
     * @param sysDictType sys dict 类型
     * @return {@link Result}
     */
    @PostMapping("/enable")
    @Log(title = "字典类型", describe = "启用", type = BusinessType.EDIT, logType = LoggingType.OPERATE)
    public Result enable(@RequestBody SysDictType sysDictType) {
        SysDictType byId = dicTypeService.getById(sysDictType.getDictTypeId());
        byId.setEnable("0");
        return decide(dicTypeService.updateById(byId), BusinessType.EDIT);
    }


    /**
     * 禁用
     *
     * @param sysDictType sys dict 类型
     * @return {@link Result}
     */
    @PostMapping("/disable")
    @Log(title = "字典类型", describe = "禁用", type = BusinessType.EDIT, logType = LoggingType.OPERATE)
    public Result disable(@RequestBody SysDictType sysDictType) {
        SysDictType byId = dicTypeService.getById(sysDictType.getDictTypeId());
        byId.setEnable("1");
        return decide(dicTypeService.updateById(byId), BusinessType.EDIT);
    }

}
